package com.mijie.homi.dao.mood;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.mijie.core.IBatisDao;
import com.mijie.homi.common.domain.mood.Mood;
import com.mijie.homi.common.domain.user.User;
import com.mijie.homi.common.util.HomiUtil;
import com.mijie.homi.domain.mood.MoodStat;

@Repository
public class MoodDao extends IBatisDao {

	public int postMood(Mood mood){
		if(mood.getVisibility()==0) mood.setVisibility(1);
		return this.insert("postMood", mood);
	}
	
	public int setTimelineIdForMood(long id,int timelineId){
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("timelineId", timelineId);
		return this.update("setTimelineIdForMood", parameter);
	}
	
	public int repostMood(Mood mood){
		return this.update("repostMood", mood);
	}
	
	public int deleteMood(long id,int userId){
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("userId", userId);
		return this.update("deleteMood",parameter);
	}

	public Map<Long,Mood> listMoodMapByIds(List<Long> list){
		return this.selectMap("listMoodMapByIds", list, "id");
	}
	
	public Mood getPrevMoodById(int userId,long id){
		Map<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("userId", userId);
		return this.selectOne("getPrevMoodById", parameter);
	}
	
	public int addMoodResponder(long moodId,int responder){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getMoodResponderTableName(moodId));
		parameter.put("moodId", moodId);
		parameter.put("userId", responder);
		return this.insert("addMoodResponder", parameter);
	}
	
	public List<User> listMoodResponder(long moodId,int limit){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getMoodResponderTableName(moodId));
		parameter.put("moodId", moodId);
		parameter.put("limit", limit);
		return this.selectList("listMoodResponder", parameter);
	}
	
	public int removeMoodResponder(long moodId,int responder){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getMoodResponderTableName(moodId));
		parameter.put("moodId", moodId);
		parameter.put("userId", responder);
		return this.delete("removeMoodResponder", parameter);
	}
	
	
	
	public int addMoodViewPrivilege(long id,int viewer){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("viewer", viewer);
		return this.insert("addMoodViewPrivilege", parameter);
	}
	
	public int setVisibility(long id,int userId,int visibility,int allowedToApply){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("userId", userId);
		parameter.put("visibility", visibility);
		parameter.put("allowedToApply", allowedToApply);
		return this.update("setVisibility", parameter);
	}
	
	public int setBroadcasted(long id){
		return this.update("setBroadcasted",id);
	}
	
	public Mood getMoodById(long id){
		return this.selectOne("getMoodById", id);
	}
	
	public int clearMoodContent(long id){
		return this.update("clearMoodContent", id);
	}
	
	public int responseMood(long id){
		return this.update("responseMood",id);
	}
	
	public int cancelResponseForMood(long id){
		return this.update("cancelResponseForMood",id);
	}
	
	
	public long getRecentyMoodId(int userId){
		return this.selectLong("getRecentyMoodId", userId);
	}
	
	public List<Mood> listMood(int userId,int offset,int limit){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("userId", userId);
		parameter.put("offset", offset);
		parameter.put("limit", limit);
		return this.selectList("listMood", parameter);
	}
	
	public List<Long> listMoodIdByViewPrivlege(int viewer,List<Long> moodIds){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("viewer", viewer);
		parameter.put("list", moodIds);
		return this.selectLongList("listMoodIdByViewPrivlege", parameter);
	}
	
	public Mood getRecentlyMood(int userId){
		return this.selectOne("getRecentlyMood", userId);
	}
	
	public int increNumOfReview(long id) {
		return this.update("increMoodNumOfReview", id);
	}

	public int decreNumOfReview(long id) {
		return this.update("decreMoodNumOfReview", id);
	}
	
	public boolean haveMoodViewPrivilege(int viewer,long id){
		Map<String,Object> parameter = new HashMap<String,Object>(2);
		parameter.put("id", id);
		parameter.put("viewer", viewer);
		Integer userId =  this.selectInteger("haveMoodViewPrivilege",parameter);
		if(userId!=null && userId==viewer) return true;
		else return false;
	}
	
	public MoodStat getMoodStatByUserId(int userId){
		return this.selectOne("getMoodStatByUserId", userId);
	}
	
	public List<Mood> listTodayMood(int userId){
		return this.selectList("listTodayMood", userId);
	}
}
